package com.mrent.web.controller.front;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.Goods;
import com.mrent.service.GoodsService;
import com.mrent.util.DateUtil;

@Controller
public class GoodsController {
	
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 查看商品.
	 * 
	 * @param goodsNo
	 * @param model
	 * @return
	 */
	@RequestMapping("/goods/{goodsNo}")
	public String lookGoods(@PathVariable String goodsNo,ModelMap model){
		Goods goods  = goodsService.getGoods(goodsNo);
		if(goods != null ){
			model.addAttribute("goods", goods);
			if(goods.getIsAuction() == 1){
				//当前日期时间.
				addNowDateTime(model);
				
				//商品开始拍卖日期时间.
				addGoodsAutionDateTime(goods,model);
				return "front/goods/auctionDetail";
			}
		}
		return "front/goods/detail";
	}
	
	//返回当前日期时间.
	private void addNowDateTime(ModelMap model){
		//取当前日期时间.
		Calendar  nowcal = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		model.addAttribute("nowyear", nowcal.get(Calendar.YEAR));
		model.addAttribute("nowmonth", nowcal.get(Calendar.MONTH)+1);
		model.addAttribute("nowday", nowcal.get(Calendar.DATE));
		model.addAttribute("nowhour", nowcal.get(Calendar.HOUR_OF_DAY));
		model.addAttribute("nowmin", nowcal.get(Calendar.MINUTE));
		model.addAttribute("nowsec", nowcal.get(Calendar.SECOND));
	}
	
	//返回商品拍卖开始日期时间.
	private void addGoodsAutionDateTime(Goods goods,ModelMap model){
		Date startDate = goods.getAuctionDate();
		Calendar startCal = DateUtil.getCalendar(startDate);
		model.addAttribute("startyear", startCal.get(Calendar.YEAR));
		model.addAttribute("startmonth", startCal.get(Calendar.MONTH)+1);
		model.addAttribute("startday", startCal.get(Calendar.DATE));
		model.addAttribute("starthour", goods.getAuctionStartHour());
		model.addAttribute("startmin", goods.getAuctionStartMin());
		model.addAttribute("startsec", (int)0);
		model.addAttribute("endhour", goods.getAuctionEndHour());
		model.addAttribute("endmin", goods.getAuctionEndMin());
		model.addAttribute("endsec", (int)0);
		int endhour = goods.getAuctionEndHour();
		int starthour = goods.getAuctionStartHour();
		int endmin = goods.getAuctionEndMin();
		int startmin = goods.getAuctionStartMin();
		int mincount = (endhour - starthour) * 60 +(endmin - startmin);
		model.addAttribute("mincount", mincount);
	}

}
